package org.example.backend.controller;


import org.example.backend.common.Result;
import org.example.backend.mapper.TagsMapper;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.RestController;

import java.util.List;
import java.util.Map;

@RestController
@RequestMapping("/api/tags")
public class TagsController {

    @Autowired
    private TagsMapper tagsMapper;

    /**
     * 获取热门标签
     * @param limit 返回数量，默认20，最大50
     * @return 热门标签列表
     */
    @GetMapping("/popular")
    public Result<List<Map<String, Object>>> getPopularTags(
            @RequestParam(value = "limit", defaultValue = "20") Integer limit
    ) {
        // 限制limit参数范围
        if (limit <= 0) {
            limit = 20;
        } else if (limit > 50) {
            limit = 50;
        }

        // 查询热门标签
        List<Map<String, Object>> popularTags = tagsMapper.findPopularTags(limit);

        return Result.success("获取热门标签成功", popularTags);
    }

}
